package com.zrlh.file_trans_monitor.controller;

import com.zrlh.file_trans_monitor.entity.TaskConfEntity;
import com.zrlh.file_trans_monitor.service.FileMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DataCURDController {
    @Autowired
    private FileMonitorService fileMonitorService;
    @RequestMapping(value = "/")
    public String index1() {
        return "errorlist";
    }

    @RequestMapping(value = "/tasklist")
    public String index2() {
        return "tasklist";
    }

    @RequestMapping(value = "/addData")
    public synchronized String addData(@RequestParam("taskName") String taskName,
                        @RequestParam("expectTime") Integer expectTime,@RequestParam("fileNameType") Integer fileNameType,
                        @RequestParam("localPath") String localPath,@RequestParam("fileName") String fileName,
                        @RequestParam("fileSuffix") String fileSuffix){
        TaskConfEntity confEntity=new TaskConfEntity();
        confEntity.setTaskName(taskName);
        confEntity.setExpectTime(expectTime);
        confEntity.setFileNameType(fileNameType);
        confEntity.setLocalPath(localPath);
        confEntity.setFileName(fileName);
        confEntity.setFileSuffix(fileSuffix);
        fileMonitorService.getTaskConfEntityList().add(confEntity);
        /*fileMonitorService.sortTaskList();*/
        fileMonitorService.writeCSV();
        fileMonitorService.setSTATUS();
        return "tasklist";
    }
    @RequestMapping(value = "/deleteData")
    public synchronized String deleteData(@RequestParam("taskName") String taskName){
        fileMonitorService.getTaskConfEntityList()
                .remove(getIndex(taskName, fileMonitorService.getTaskConfEntityList()));
        fileMonitorService.writeCSV();
        return "tasklist";
    }
    @RequestMapping(value = "/updateData")
    public synchronized String updateData(@RequestParam("taskName") String taskName,
                                          @RequestParam("expectTime") Integer expectTime,@RequestParam("fileNameType") Integer fileNameType,
                                          @RequestParam("localPath") String localPath,@RequestParam("fileName") String fileName,
                                          @RequestParam("fileSuffix") String fileSuffix){
        TaskConfEntity confEntity=new TaskConfEntity();
        confEntity.setTaskName(taskName);
        confEntity.setExpectTime(expectTime);
        confEntity.setFileNameType(fileNameType);
        confEntity.setLocalPath(localPath);
        confEntity.setFileName(fileName);
        confEntity.setFileSuffix(fileSuffix);
        fileMonitorService.getTaskConfEntityList()
                .remove(getIndex(taskName,fileMonitorService.getTaskConfEntityList()));
        fileMonitorService.getTaskConfEntityList().add(confEntity);
        /*fileMonitorService.sortTaskList();*/
        fileMonitorService.writeCSV();
        fileMonitorService.setSTATUS();
        return "tasklist";
    }
    @RequestMapping(value = "/deleteErrorData")
    public synchronized String deleteExceptionData(@RequestParam("taskName") String taskName){
        fileMonitorService.getERRORList().remove(getIndex(taskName,fileMonitorService.getERRORList()));
        return "errorlist";
    }
    int getIndex(String taskName, List<TaskConfEntity> list){
        int index=0;
        for(TaskConfEntity item:list){
            if(item.getTaskName().equals(taskName))
                break;
            index++;
        }
        return index;
    }
}
